package org.hwua.production.service.impl;

import java.util.List;

import org.hwua.production.domain.Customer;
import org.hwua.production.domain.CustomerExample;
import org.hwua.production.domain.custom.CustomResult;
import org.hwua.production.domain.custom.EUDataGridResult;
import org.hwua.production.mapper.CustomerMapper;
import org.hwua.production.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
/**
 * 客户管理业务逻辑层实现类
 * @author hwua
 *
 */
@Service
public class CustomerServiceImpl implements CustomerService{

	@Autowired
	CustomerMapper customerMapper;
	
	@Override
	public EUDataGridResult getList(int page, int rows, Customer custom) throws Exception{
		//查询客户列表
		CustomerExample example = new CustomerExample();
		//分页处理
		PageHelper.startPage(page, rows);
		List<Customer> list = customerMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		//取记录总条数
		PageInfo<Customer> pageInfo = new PageInfo<Customer>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

	@Override
	public Customer get(String string) throws Exception{
		return customerMapper.selectByPrimaryKey(string);
	}

	@Override
	public CustomResult delete(String string) throws Exception{
		int i = customerMapper.deleteByPrimaryKey(string);
		if(i>0){
			return CustomResult.ok();
		}else{
			return null;
		}
	}

	@Override
	public CustomResult deleteBatch(String[] ids) throws Exception{
		int i = customerMapper.deleteBatch(ids);
		if(i>0){
			return CustomResult.ok();
		}else{
			return null;
		}
	}

	@Override
	public CustomResult insert(Customer custom) throws Exception{
		int i = customerMapper.insert(custom);
		if(i>0){
			return CustomResult.ok();
		}else{
			return CustomResult.build(101, "新增客户失败");
		}
	}

	@Override
	public CustomResult update(Customer custom) throws Exception{
		int i = customerMapper.updateByPrimaryKeySelective(custom);
		if(i>0){
			return CustomResult.ok();
		}else{
			return CustomResult.build(101, "修改客户失败");
		}
	}
	
	@Override
	public CustomResult updateAll(Customer custom) throws Exception{
		int i = customerMapper.updateByPrimaryKey(custom);
		if(i>0){
			return CustomResult.ok();
		}else{
			return CustomResult.build(101, "修改客户失败");
		}
	}

	@Override
	public CustomResult updateNote(Customer custom) throws Exception{
		int i = customerMapper.updateNote(custom);
		if(i>0){
			return CustomResult.ok();
		}else{
			return CustomResult.build(101, "更新客户介绍失败");
		}
	}

	@Override
	public CustomResult changeStatus(String[] ids) throws Exception{
		customerMapper.changeStatus(ids);
		return CustomResult.ok();
	}

	@Override
	public List<Customer> find() throws Exception{
		CustomerExample example = new CustomerExample();
		return customerMapper.selectByExample(example);
	}

	@Override
	public EUDataGridResult searchCustomerByCustomName(int page, int rows, String customName) throws Exception{
		//分页处理
		PageHelper.startPage(page, rows);
		List<Customer> list =  customerMapper.searchCustomerByCustomName(customName);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		//取记录总条数
		PageInfo<Customer> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}
	
	@Override
	public EUDataGridResult searchCustomerByCustomId(int page, int rows, String customId) throws Exception{
		//分页处理
		PageHelper.startPage(page, rows);
		List<Customer> list =  customerMapper.searchCustomerByCustomId(customId);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		//取记录总条数
		PageInfo<Customer> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}
}
